package com.css.fxfzypg.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class PlatformDateUtils {
    private static ThreadLocal<java.text.DateFormat> threadLocal = new ThreadLocal();

    private static java.text.DateFormat getDateFormater(PlatformDateUtils.DateFormat format) {
        SimpleDateFormat dateFormater = (SimpleDateFormat)threadLocal.get();
        if (dateFormater == null) {
            dateFormater = new SimpleDateFormat(format.format);
            threadLocal.set(dateFormater);
        } else if (!format.format.equals(dateFormater.toPattern())) {
            dateFormater = new SimpleDateFormat(format.format);
            threadLocal.set(dateFormater);
        }

        return dateFormater;
    }

    public static String parseString(Date date) {
        return getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_HYPHEN).format(date);
    }

    public static String parseString(Date date, PlatformDateUtils.DateFormat format) {
        return getDateFormater(format).format(date);
    }

    public static String parseString(java.sql.Date date) {
        return getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_HYPHEN).format(date);
    }

    public static String parseString(java.sql.Date date, PlatformDateUtils.DateFormat format) {
        return getDateFormater(format).format(date);
    }

    public static String parseString(Timestamp time) {
        return getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_HYPHEN).format(time);
    }

    public static String parseString(Timestamp time, PlatformDateUtils.DateFormat format) {
        return getDateFormater(format).format(time);
    }

    public static String parseString(Calendar cal) {
        return getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_HYPHEN).format(cal.getTime());
    }

    public static String parseString(Calendar cal, PlatformDateUtils.DateFormat format) {
        return getDateFormater(format).format(cal.getTime());
    }

    public static String parseString(long millisecond) {
        return getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_HYPHEN).format(new Date(millisecond));
    }

    public static String parseString(long millisecond, PlatformDateUtils.DateFormat format) {
        return getDateFormater(format).format(new Date(millisecond));
    }

    public static Date parseDate(java.sql.Date date) {
        return new Date(date.getTime());
    }

    public static Date parseDate(Timestamp time) {
        return new Date(time.getTime());
    }

    public static Date parseDate(Calendar cal) {
        return cal.getTime();
    }

    public static Date parseDate(long millisecond) {
        return new Date(millisecond);
    }

    public static Date parseDate(String dateStr) {
        Date date = null;
        String str = null;
        str = "yyyy-MM-dd HH:mm:ss";
        if (date == null && str.length() == dateStr.length() && !dateStr.contains("/") && dateStr.contains(":") && dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_HYPHEN).parse(dateStr);
            } catch (Exception var13) {
                var13.printStackTrace();
            }
        }

        str = "yyyy/MM/dd HH:mm:ss";
        if (date == null && str.length() == dateStr.length() && dateStr.contains("/") && dateStr.contains(":") && !dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_SLASH).parse(dateStr);
            } catch (Exception var12) {
                var12.printStackTrace();
            }
        }

        str = "yyyy年MM月dd日HH时mm分ss秒";
        if (date == null && str.length() == dateStr.length() && dateStr.contains("年") && dateStr.contains("月") && dateStr.contains("日") && dateStr.contains("时") && dateStr.contains("分") && dateStr.contains("秒")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_CN).parse(dateStr);
            } catch (Exception var11) {
                var11.printStackTrace();
            }
        }

        str = "yyyyMMddHHmmss";
        if (date == null && str.length() == dateStr.length() && !dateStr.contains("/") && !dateStr.contains(":") && !dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_NONE).parse(dateStr);
            } catch (Exception var10) {
                var10.printStackTrace();
            }
        }

        str = "yyyy-MM-dd";
        if (date == null && str.length() == dateStr.length() && !dateStr.contains("/") && !dateStr.contains(":") && dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMD_HYPHEN).parse(dateStr);
            } catch (Exception var9) {
                var9.printStackTrace();
            }
        }

        str = "yyyy/MM/dd";
        if (date == null && str.length() == dateStr.length() && dateStr.contains("/") && !dateStr.contains(":") && !dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMD_SLASH).parse(dateStr);
            } catch (Exception var8) {
                var8.printStackTrace();
            }
        }

        str = "yyyyMMdd";
        if (date == null && str.length() == dateStr.length() && !dateStr.contains("/") && !dateStr.contains(":") && !dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMD_NONE).parse(dateStr);
            } catch (Exception var7) {
                var7.printStackTrace();
            }
        }

        str = "yyyy年MM月dd日";
        if (date == null && str.length() == dateStr.length() && dateStr.contains("年") && dateStr.contains("月") && dateStr.contains("日")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMD_CN).parse(dateStr);
            } catch (Exception var6) {
                var6.printStackTrace();
            }
        }

        str = "yyyy-MM-dd HH:mm:ss:SSS";
        if (date == null && str.length() == dateStr.length() && !dateStr.contains("/") && dateStr.contains(":") && dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMDHMS_WITH_MS_HYPHEN).parse(dateStr);
            } catch (Exception var5) {
                var5.printStackTrace();
            }
        }

        str = "yyyy-MM-dd HH:mm";
        if (date == null && str.length() == dateStr.length() && !dateStr.contains("/") && dateStr.contains(":") && dateStr.contains("-")) {
            try {
                date = getDateFormater(PlatformDateUtils.DateFormat.YMDHM_HYPHEN).parse(dateStr);
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return date;
    }

    public static Date parseDate(String dateStr, PlatformDateUtils.DateFormat format) {
        Date date = null;

        try {
            date = getDateFormater(format).parse(dateStr);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return date;
    }

    public static java.sql.Date parseSqlDate(Date date) {
        return new java.sql.Date(date.getTime());
    }

    public static java.sql.Date parseSqlDate(Timestamp time) {
        return new java.sql.Date(time.getTime());
    }

    public static java.sql.Date parseSqlDate(Calendar cal) {
        return new java.sql.Date(cal.getTimeInMillis());
    }

    public static java.sql.Date parseSqlDate(long millisecond) {
        return new java.sql.Date(millisecond);
    }

    public static java.sql.Date parseSqlDate(String dateStr) {
        Date date = parseDate(dateStr);
        return date == null ? null : new java.sql.Date(date.getTime());
    }

    public static java.sql.Date parseSqlDate(String dateStr, PlatformDateUtils.DateFormat format) {
        Date date = parseDate(dateStr, format);
        return date == null ? null : new java.sql.Date(date.getTime());
    }

    public static Timestamp parseTimestamp(Date date) {
        return new Timestamp(date.getTime());
    }

    public static Timestamp parseTimestamp(java.sql.Date date) {
        return new Timestamp(date.getTime());
    }

    public static Timestamp parseTimestamp(Calendar cal) {
        return new Timestamp(cal.getTimeInMillis());
    }

    public static Timestamp parseTimestamp(long millisecond) {
        return new Timestamp(millisecond);
    }

    public static Timestamp parseTimestamp(String dateTimeStr) {
        Date date = parseDate(dateTimeStr);
        return date == null ? null : new Timestamp(date.getTime());
    }

    public static Timestamp parseTimestamp(String dateStr, PlatformDateUtils.DateFormat format) {
        Date date = parseDate(dateStr, format);
        return date == null ? null : new Timestamp(date.getTime());
    }

    public static long parseMillisecond(Date date) {
        return date.getTime();
    }

    public static long parseMillisecond(java.sql.Date date) {
        return date.getTime();
    }

    public static long parseMillisecond(Calendar cal) {
        return cal.getTimeInMillis();
    }

    public static long parseMillisecond(Timestamp time) {
        return time.getTime();
    }

    public static long parseMillisecond(String dateStr) {
        Date date = parseDate(dateStr);
        return date == null ? -1L : date.getTime();
    }

    public static long parseMillisecond(String dateStr, PlatformDateUtils.DateFormat format) {
        Date date = parseDate(dateStr, format);
        return date == null ? -1L : date.getTime();
    }

    public static Calendar parseCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar parseCalendar(java.sql.Date sqlDate) {
        Date date = parseDate(sqlDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar parseCalendar(Timestamp time) {
        Date date = parseDate(time);
        Calendar var2 = Calendar.getInstance();
        var2.setTime(date);
        return var2;
    }

    public static Calendar parseCalendar(long millisecond) {
        Date date = parseDate(millisecond);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar parseCalendar(String dateStr) {
        Date date = parseDate(dateStr);
        if (date == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        }
    }

    public static Calendar parseCalendar(String dateStr, PlatformDateUtils.DateFormat format) {
        Date date = parseDate(dateStr, format);
        if (date == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        }
    }

    public static boolean isLeapYear(Integer year){
        Integer r = 4;
        Integer h = 100;
        if (year % r == 0 && year % h != 0) {
            return true;
        }else if(year % (r*h) == 0 ){
            return true;
        }else{
            return false;
        }
    }


    public static Timestamp getCurrentTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    public static Date getCurrentDate() {
        return new Date();
    }

    public static java.sql.Date getCurrentSqlDate() {
        return new java.sql.Date(System.currentTimeMillis());
    }

    public static long getCurrentMillisecond() {
        return System.currentTimeMillis();
    }

    public static Calendar getCurrentCalendar() {
        return Calendar.getInstance();
    }

    public static String getCurrentDateStr() {
        Date var0 = new Date();
        return parseString(var0);
    }

    public static String getCurrentDateStr(PlatformDateUtils.DateFormat format) {
        Date var1 = new Date();
        return parseString(var1, format);
    }

    public static int calcMonthDays(Calendar date) {
        Calendar t1 = (Calendar)date.clone();
        Calendar t2 = (Calendar)date.clone();
        int year = date.get(1);
        int var4 = date.get(2);
        t1.set(year, var4, 1);
        t2.set(year, var4 + 1, 1);
        t2.add(6, -1);
        return calcDays(t2, t1) + 1;
    }

    private static int calcDays(long t1, long t2) {
        long millis = t1 - t2;
        return millis == 0L ? 0 : (int)(millis / 86400000L);
    }

    public static int calcDays(Calendar c1, Calendar c2) {
        return calcDays(c1.getTimeInMillis(), c2.getTimeInMillis());
    }

    public static Calendar lastDay(Calendar c) {
        Calendar t = (Calendar)c.clone();
        t.set(5, 1);
        t.add(2, 1);
        t.add(5, -1);
        return t;
    }

    public static String parse8CharString(Date date) {
        String result = null;
        if (date != null) {
            result = parseString(date, PlatformDateUtils.DateFormat.YMD_NONE);
        }

        return result;
    }

    public static String parse14CharString(Date date) {
        String result = null;
        if (date != null) {
            result = parseString(date, PlatformDateUtils.DateFormat.YMDHMS_NONE);
        }

        return result;
    }

    public static Date parse8CharDate(String date) {
        return parseDate(date, PlatformDateUtils.DateFormat.YMD_NONE);
    }

    public static Date parse14CharDate(String date) {
        return parseDate(date, PlatformDateUtils.DateFormat.YMDHMS_NONE);
    }

    public static Date addDay(Date date, int day) {
        if (date != null) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(5, day);
            date = c.getTime();
        }

        return date;
    }

    public static enum DateFormat {
        YMDHMS_HYPHEN("yyyy-MM-dd HH:mm:ss"),
        YMDHMS_SLASH("yyyy/MM/dd HH:mm:ss"),
        YMDHMS_CN("yyyy年MM月dd日HH时mm分ss秒"),
        YMDHMS_NONE("yyyyMMddHHmmss"),
        YMD_HYPHEN("yyyy-MM-dd"),
        YMD_SLASH("yyyy/MM/dd"),
        YMD_NONE("yyyyMMdd"),
        YMD_CN("yyyy年MM月dd日"),
        YMDHMS_WITH_MS_HYPHEN("yyyy-MM-dd HH:mm:ss.SSS"),
        YMDHM_HYPHEN("yyyy-MM-dd HH:mm");

        private String format;

        private DateFormat(String format) {
            this.format = format;
        }
    }
}
